package com.wuxu.lib_skin_core.res

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.res.Resources
import android.os.AsyncTask
import android.os.Build
import android.util.SparseArray
import com.wuxu.lib_skin_core.constant.SKIN_INNER_TYPE
import com.wuxu.lib_skin_core.load.SkinLoadStrategyFactory

/**
 *@author wuxu
 *@date 2024/5/21 10:44
 *@description Skin 管理者
 */
object SkinManager : Observer {

    private val observableSet: HashSet<Observable> by lazy { HashSet() }

    override fun registerObservable(observable: Observable) {
        observableSet.add(observable)
    }

    override fun unregisterObservable(observable: Observable) {
        observableSet.remove(observable)
    }


    fun notifySkinUpdate() {
        observableSet.forEach { it.update() }
    }

    fun loadSkin(context: Context, skinName: String, skinLoadStrategyType: Byte = SKIN_INNER_TYPE, skinLoaderListener:SkinLoaderListener?=null){
        val skinLoadStrategy = SkinLoadStrategyFactory.getSkinLoadStrategy(skinLoadStrategyType)
        skinLoadStrategy.loadSkinInBackground(context,skinName)
    }


    class SkinLoadTask() : AsyncTask<String, Void, String>() {
        override fun onPreExecute() {
            super.onPreExecute()
        }

        override fun onCancelled() {
            super.onCancelled()
        }

        override fun onPostExecute(result: String?) {
            super.onPostExecute(result)
        }

        override fun doInBackground(vararg params: String?): String {
            return params[0]?:""
        }
    }


    /**
     * 皮肤加载
     */
    interface SkinLoaderListener {

        /**
         * 开始加载.
         */
        fun onStart()

        /**
         * 加载成功.
         */
        fun onSuccess()

        /**
         * 加载失败.
         *
         * @param errMsg 错误信息.
         */
        fun onFailed(errMsg: String?)
    }
}


interface Observer {
    fun registerObservable(observable: Observable)

    fun unregisterObservable(observable: Observable)
}

interface Observable {
    fun update(data: Any? = null)
}